package com.boboPlanet.dict.entity.po;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO 单例测试
 * @Author: mis_wu
 * @Date: 2023/8/5
 * @Company Singleton
 **/
public class SingletonTest implements Cloneable {

    private static final SingletonTest instance = null;

    private Long Id;

    public Long getId(){
        return Id;
    }

    /**
     * 浅克隆
     * @return Object
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    /**
     * 深复制
     * @return Object
     */
    public Object deepClone() throws IOException, ClassNotFoundException {

        //写入当前对象的二进制流
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);

        // 读出二进制流产生的新对象
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return ois.readObject();
    }

    /**
     * 此处使用一个内部类来维护单例
     */
    private static class SingletonTestFactory {
        private static final SingletonTest instance = new SingletonTest();
    }

    /* 获取实例 */
    public static SingletonTest getInstance() {
        return SingletonTestFactory.instance;
    }

    public void updateProperties() {
        SingletonTest instance = new SingletonTest();
        Id = instance.getId();
    }

    /**
     * 如果该对象被用于序列化，可以保证对象在序列化前后保持一致
     * @return SingletonTest
     */
    public Object readResolve() {
        return getInstance();
    }


//    public static SingletonTest getInstance(){
//        if (instance == null){
//            synchronized (instance){
//                if (instance == null){
//                    instance = new SingletonTest();
//                }
//            }
//        }
//
//        return instance;
//    }


    public static void main(String[] args) {
        SingletonTest instance1 = SingletonTest.getInstance();

    }

}
